<template>
	<view>
		<view class="navBar">
			<view class="left">
				<uni-icons type="left" size="24" @click="toBack"></uni-icons>
			</view>
			<view class="center">修改密码</view>
			<view class="right"></view>
		</view>
		<view class="headText">
			<view class="changePsw-cn">修改密码</view>
			<view class="changePsw-en">Change password here</view>
		</view>
		<view class="changePswBox">
			<view class="passwordInput">
				<view class="">新密码:</view>
				<input type="password" v-model="newPassword" placeholder="请输入密码" />
			</view>
			<view class="passwordInput">
				<view class="">确认密码:</view>
				<input type="password" v-model="confirmPassword" placeholder="请输入密码" />
			</view>
			<button class="confirm-btn" type="primary" @click="changePsw">确认</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				newPassword: '',
				confirmPassword: '',
				user: {}
			}
		},
		onLoad() {
			this.user = uni.getStorageSync("user");
			this.token = uni.getStorageSync("token");
		},
		methods: {
			toBack() {
				uni.navigateBack({
					delta: 1,
				});
			},
			toLogin() {
				uni.navigateTo({
					url: '/pages/login/login',
				});
			},
			changePsw() {
				if (this.newPassword != this.confirmPassword) {
					uni.showToast({
						icon: "none",
						title: "两次密码不一致",
					});
					return;
				}
				uni.request({
					url: 'http://localhost:8080/changeUserPsw',
					method: 'POST',
					header: {
						'Authorization': this.token,
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					data: {
						userId: this.user.userId,
						password: this.newPassword,
					},
					success: res => {
						console.log(res.data);
						if (res.data.code == 200) {
							uni.showToast({
								icon: "success",
								title: "已成功修改密码"
							});
							this.toLogin();
						} else {
							uni.showToast({
								icon: "error",
								title: "修改失败"
							})
						}
					},
					fail: () => {},
					complete: () => {}
				});
			}
		}
	}
</script>


<style lang="scss">
	.navBar {
		display: flex;
		height: 100rpx;
		background-color: white;
		align-items: center;
		padding: 0 30rpx;
		/* #ifdef MP-WEIXIN */
		padding-top: 100rpx;
		/* #endif */

		.left {
			flex: 1;
		}

		.center {
			flex: 1;
			color: #646566;
			text-align: center;
			font-size: 34rpx;
			font-weight: 700;
		}

		.right {
			flex: 1;
		}
	}

	.headText {
		margin: 140rpx 30rpx;

		.changePsw-cn {
			margin-bottom: 30rpx;
			font-size: 52rpx;
			color: #646566;
			font-weight: 700;
		}

		.changePsw-en {
			font-size: 38rpx;
			font-weight: 400;
			color: #83838b;
		}

	}

	.changePswBox {
		margin: 0 50rpx;

		.passwordInput {
			display: flex;
			align-items: center;
			padding: 30rpx 0;
			border-bottom: 1rpx solid #ccc;

			view {
				flex: 1;
			}

			input {
				flex: 3;
			}
		}

		.confirm-btn {
			margin-top: 50rpx;
			border-radius: 50rpx;
		}
	}
</style>